/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

import org.marcpoppleton.strawberry.constants.Preferences;
import org.marcpoppleton.strawberry.provider.Coc;
import org.marcpoppleton.strawberry.provider.Unit;

public class SettingsActivity extends PreferenceActivity {

	private ListPreference mCoc;
	private ListPreference mDistances;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getPreferenceManager().setSharedPreferencesName(Preferences.NAME);
		addPreferencesFromResource(R.layout.preferences_screen);
		setupViews();
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		setupViews();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_settings, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings_item_manage_cameras:
			CamerasActivity.show(this);
			return true;
		case R.id.menu_settings_item_manage_units:
			UnitsActivity.show(this);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	} 	

	private void setupViews() {

		final Cursor cCoc = this.managedQuery(Coc.CONTENT_URI,
				Coc.PROJECTION_IDS_AND_ALL, null, null, Coc.DEFAULT_SORT_ORDER);
		int count = cCoc.getCount();
		CharSequence[] entries = new CharSequence[count];
		cCoc.moveToFirst();
		for (int i = 0; i < count; i++) {
			entries[i] = cCoc.getString(cCoc.getColumnIndexOrThrow(Coc._ID));
			cCoc.moveToNext();
		}
		mCoc = (ListPreference) findPreference(Preferences.KEY_CAMERA);
		mCoc.setEntries(entries);
		mCoc.setEntryValues(entries);

		final Cursor cUnits = this.managedQuery(Unit.CONTENT_URI,
				Unit.PROJECTION_IDS_AND_ALL, null, null, Unit.DEFAULT_SORT_ORDER);
		count = cUnits.getCount();
		entries = new CharSequence[count];
		cUnits.moveToFirst();
		for (int i = 0; i < count; i++) {
			entries[i] = cUnits.getString(cUnits.getColumnIndexOrThrow(Coc._ID));
			cUnits.moveToNext();
		}
		mDistances = (ListPreference) findPreference(Preferences.KEY_UNITS);
		mDistances.setEntries(entries);
		mDistances.setEntryValues(entries);
	}

	/**
	 * Starts the PreferencesActivity for the specified user.
	 * 
	 * @param context
	 *            The application's environment.
	 */
	 static void show(Context context) {
		 final Intent intent = new Intent(context, SettingsActivity.class);
		 context.startActivity(intent);
	 }
}
